package com.icode.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.icode.common.utils.R;
import com.icode.gulimall.ware.feign.MemberFeignService;
import com.icode.gulimall.ware.vo.FareVo;
import com.icode.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.Query;

import com.icode.gulimall.ware.dao.WareInfoDao;
import com.icode.gulimall.ware.entity.WareInfoEntity;
import com.icode.gulimall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and(wapper->wapper
                    .eq("id", key)
                    .or().like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key)
            );
        }
        IPage<WareInfoEntity> page = this.page(
                new  Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        //获取用户的详细地址
        FareVo fareVo = new FareVo();
        R addrInfo = memberFeignService.getAddrInfo(addrId);
        if (addrInfo.getCode()==0){
            MemberAddressVo addrInfoData = addrInfo.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
            });
            fareVo.setAddress(addrInfoData);
            //todo 调用物流运费查询接口 此处默认运费为9.90
            fareVo.setFare(new BigDecimal(9.90));
        }
        return fareVo;
    }

}